package cn.itsource.system.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.itsource.system.domain.Log;
import cn.itsource.system.mapper.LogMapper;
import cn.itsource.system.query.MenuQuery;
import cn.itsource.system.service.LogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class LogServiceImpl implements LogService {

    @Autowired
    private LogMapper logMapper;

    @Override
    public List<Log> findAll() {
        return logMapper.findAll();
    }

    //批量删除
    @Override
    public void patchdel(Long[] ids) {
        //articleMapper.patchdel(ids);
        for (Long id : ids) {
            delete(id);
        }
    }

    @Transactional
    @Override
    public void delete(Long id) {
         logMapper.delete(id);
    }

    @Override
    public PageInfo<Log> pageQuery(MenuQuery query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<Log> logs = logMapper.pageQuery(query);
        return new PageInfo<>(logs);
    }

    //导出文件
    @Override
    public void exportFile(Long[] ids) { //ids
        ArrayList<Log> arrayList = new ArrayList<Log>();
        for (Long id : ids) {
            Log log = logMapper.findOne(id);
            arrayList.add(log);
        }
        ArrayList<Log> logs = CollUtil.newArrayList(arrayList);
        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter("d:/log.xlsx");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(logs, true);
        // 关闭writer，释放内存
        writer.close();
    }
}
